package com.jiyiren.retrofitdemo.utils.tool;

import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Author:    ZhuWenWu
 * Version    V1.0
 * Date:      14-11-22 14:32
 * Description: 字符串帮助类
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 14-11-22      ZhuWenWu            1.0                    1.0
 * Why & What is modified:
 */
public class StringHelper {
    public static String getStr(String str) {
        return isEmpty(str) ? "" : str;
    }

    public static String getEditTextContent(EditText edt) {
        return edt.getText().toString().trim();
    }

    public static String getTextViewContent(TextView tv) {
        return tv.getText().toString().trim();
    }

    public static boolean isEmpty(String str) {
        return str == null || str.equals("null") || str.trim().equals("");
    }

    public static boolean isEditTextEmpty(EditText edt) {
        return isEmpty(getEditTextContent(edt));
    }

    public static boolean isPhoneEditTextEmpty(EditText edt) {
        return isEmpty(getEditTextContent(edt)) || getEditTextContent(edt).length() != 11;
    }

    public static boolean notEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 去除大部分的空格，制表符，回车等空格
     */
    public static String getDeleteSpaceStr(String str){
        return str.replaceAll("\\s*", "");
    }


    /**
     * 严格判断字符串的是否为空
     *
     * @param input
     * @return 0不为空且没有空字符串，1为空，2不为空但是包含空字符
     */
    public static int isStrictEmpty(String input) {
        if ( input == null || "".equals( input ) ||input.length()==0)
            return 1;
        for ( int i = 0; i < input.length(); i++ ) {
            char c = input.charAt( i );
            if ( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) {
                return 2;

            }
        }
        return 0;
    }

    /**
     * 获得数量的中文简化表示
     * @param count
     * @return
     */
    public static String getTextLikeCount(long count) {
        String likeText = String.valueOf(count);
        if(count>10000000){
            likeText = String.format("%1$.2f 千万", count / 10 * 1000 * 1000f);
        }else if(count>10000){
            likeText = String.format("%1$.2f 万", count / 10 * 1000f);
        }else if(count>1000){
            likeText = String.format("%1$.1f K", count / 1000f);
        }else{
            likeText = count+"";
        }

        return likeText;
    }


    /**
     * 用原生方法，根据key获得Json中的value
     * @param json
     * @param value 这里是为了节约内存，value既用作"键"也用作"值"
     * @return
     */
    public static String geJson(String json, String value) {
        try {
            JSONObject dataJson = new JSONObject(json);
            value = dataJson.getString(value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return value;
    }
}
